<meta charset='utf-8' />
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" />

<div>
	<input id='openid_inp' />
	<button type='button' id='Jump'>跳转</button>
	<button type='button' id='Refresh'>刷新</button>
</div>

<script src='../Public/jquery.js'></script>
<script src='../Public/fun.js'></script>
<script>
expire = 5000;

Refresh.onclick=function(){
	localStorage.openid = '';
	location.reload();
}

Jump.onclick=function(){
	Init();
}



function Init(){
	if(localStorage.openid){	//有openid,则\获取商户信息
		var openid = localStorage.openid;
		if(parseInt(localStorage.OpenidExpire) + expire < new Date().getTime()){
			C('expire');
			localStorage.openid = '';
		}
		C('GetOpenid_Sess');
		GetStoreInfo(openid);
	}else{						//无openid,则获取openid

		C('GetOpenid_After');
		var openid = GetOpenid();
		C(openid);
		if(openid){
			localStorage.openid = openid;
			localStorage.OpenidExpire = new Date().getTime();
			GetStoreInfo(localStorage.openid); 	//通过openid获取商户信息
		}else{
			C('111'); //到不了这里，当获取openid失败时会直接跳转到error.html
		}
	}
}


//通过openid获取商户信息
function GetStoreInfo(openid){
	//var url = 'http://shop-cache-upyun.bm123.top/Openid/'+openid+'.html';
	var url = 'http://tp-shop-cache.bm123.top/Openid/'+openid+'.html';

	UrlIsActive(url,function(stat){
		//商户upyun缓存信息存在，则缓存到sessionStorage,并跳转到商户首页
		if(stat){	
			CorsQuery(url,'Gets',function(res){
				//C(res);return;
				if(res.id){	//信息完整，缓存并跳转index.html
					sessionStorage.StoreInfo = JSON.stringify(res);
					var db_info = res.city_code+','+res.db_host+','+res.db_name;
					var fd2 = new FormData();
					fd2.append('type','SetDbInfo');
					fd2.append('db_info',db_info);
					fd2.append('store_id',res.id);
					fd2.append('store_name',res.name);
					Ajax_FormData2(fd2,'interface.php',function(res){
						//C(res);return;
						if(res == 'ok'){
							location.replace('index.html');
						}else{
							location.replace('../error.html?t=400007');
							//400007 请使用微信浏览器!
						}
					
					});
				}else{
					//商户upyun缓存不存在，则向全局库获取openid对应县服信息
					GetStoreInfo2(openid);
				}
			},2000);
		}else{		
			//商户upyun缓存不存在，则向全局库获取openid对应县服信息
			GetStoreInfo2(openid);	//获取商户信息
		}
	});
}


function GetStoreInfo2(openid){		//向全局库请求商户数据
	var fd = new FormData();
	fd.append('type','GetStoreInfo');
	fd.append('openid',openid);
	Ajax_FormData2(fd,'interface.php',function(res){
		if(/^{"id":.*}$/.test(res)){	//如果数据库有商户信息缓存到sessionStorage,
			sessionStorage.StoreInfo = res;
			location.replace('index.html');
		}
		switch(res){
			case 'err4': 
				//全局服没有记录，跳转到注册页
				location.replace('register.html');
				break;
			case 'err3':
				//400008 商户信息全局库有记录，但全局缓存及县服无商户信息，数据丢失
				location.replace('../error.html?t=400008');
				break;
			case 'err2':
				//400009 县服参数异常
				location.replace('../error.html?t=400009');
				break;
			case 'err1':
				//400010 缺少openid
				location.replace('../error.html?t=400010');
				break;
		}

	});
}



function GetOpenid(){
	var openid = openid_inp.value.trim()
	openid = openid?openid:'bb1';

	return openid;
/*	if(IsWxWeb()){	//是微信浏览器，则获取微信openid
		var back_uri = encodeURIComponent(document.URL);	//获取当前页面url
		if(GetUrlParm('get_openid_res')){
			var res = GetUrlParm('get_openid_res');
			if(res && /^{.*}$/.test(res)){
				var res2 = JSON.parse(decodeURIComponent(res));
				var openid = res2.openid;	//获取到openid
				return openid;
			}else{
				//location.replace('../error.html?t=400002'); 
				//400002微信数据异常，请稍候再再试！
			}
		}else{
			location.href='http://tools.bm123.top/wx_login/wx_js.php?get_openid_back_uri='+back_uri+'&t=t';
		}
	}else{
		//location.replace('../error.html?t=400001');	//400001 请使用微信浏览器!
	}*/
}


</script>